/**
 * @FileName: RadaSettingsActivity.java
 * @Package:com.bdtop.dbl.ui.activities.device
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-29
 * @version V1.0
 */

package com.bdtop.dbl.bluetooth;

import android.app.Activity;
import android.app.Dialog;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.bdtop.dbl.DBLApplication;
import com.bdtop.dbl.R;
import com.bdtop.dbl.ui.activities.main.MainActivity;
import com.bdtop.dbl.ui.activities.main.SplashScreenActivity;
import com.bdtop.dbl.utils.DBLDebug;

/**
 * @ClassName: BleDeviceWarningActivity
 * @Description: TODO
 * @author: wangpeng
 * @date:2014-11-14
 */
public class BleDeviceWarningActivity extends Activity {
	// !!!!!!!!!!! do not to extends baseActity !!!!!!!!!!!!!!!!!

	public static final void launch(Context context) {
		Intent intent = new Intent(context, BleDeviceWarningActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_ble_device_warning);
		super.onCreate(savedInstanceState);

		// set this flag so this activity will stay in front of the keyguard
		// Have the WindowManager filter out touch events that are "too fat".
		int flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES;

		getWindow().addFlags(flags);

		acquireWakeLock();

		KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
		KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("unlock");
		keyguardLock.disableKeyguard();

		bindBLeService();
	}

	private BluetoothLeService mBluetoothLeService;
	private boolean isBindBleService = false;

	@SuppressWarnings("static-access")
	private void bindBLeService() {
		if (!isBindBleService) {
			isBindBleService = true;
			Intent gattServiceIntent = new Intent(this, BluetoothLeService.class);
			this.bindService(gattServiceIntent, mServiceConnection, this.BIND_AUTO_CREATE);
		}
	}

	private void unBindBLeService() {
		if (isBindBleService) {
			isBindBleService = false;
			this.unbindService(mServiceConnection);
			mBluetoothLeService = null;
		}
	}

	private final ServiceConnection mServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName componentName, IBinder service) {
			DBLDebug.logd("Radar Mode onServiceConnected");

			mBluetoothLeService = ((BluetoothLeService.LocalBinder) service).getService();

			showWarningDialog();
		}

		@Override
		public void onServiceDisconnected(ComponentName componentName) {
			mBluetoothLeService = null;
		}
	};

	private void showWarningDialog() {
		final Dialog d = new Dialog(this, DBLApplication.getInstance().isAppLife() ? R.style.Theme_Light_NoTitle_Dialog
				: R.style.Theme_Light_NoTitle_NoShadow_Dialog);
		d.setCancelable(false);
		d.setCanceledOnTouchOutside(false);

		OnClickListener l = new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				if (arg0.getId() == R.id.btn_confirm) {
					DBLDebug.logd("is app already open :" + DBLApplication.getInstance().isAppLife());
					// app already open
					SplashScreenActivity.launch(BleDeviceWarningActivity.this, MainActivity.WHAT_FROM_BLE_SERVICE);
				} else if (arg0.getId() == R.id.btn_cancel) {
					// mBluetoothLeService.setPauseToRemind(300 * 1000);
				}

				mBluetoothLeService.stopPlayMedia();

				d.dismiss();
				BleDeviceWarningActivity.this.finish();
			}
		};

		View view = LayoutInflater.from(this).inflate(R.layout.dialog_ble_device_warning, null);
		TextView txMessage = (TextView) view.findViewById(R.id.tx_message);
		txMessage.setText(R.string.warning_protect);

		Button btnCancel = (Button) view.findViewById(R.id.btn_cancel);
		btnCancel.setVisibility(View.VISIBLE);
		btnCancel.setText(R.string.ok_ikown);
		btnCancel.setOnClickListener(l);

		Button btnConfirm = (Button) view.findViewById(R.id.btn_confirm);
		btnConfirm.setVisibility(View.VISIBLE);
		btnConfirm.setText(R.string.to_look);
		btnConfirm.setOnClickListener(l);

		if (DBLApplication.getInstance().isAppLife()) {
			btnConfirm.setVisibility(View.GONE);
			view.findViewById(R.id.btn_divider).setVisibility(View.GONE);
		}

		d.setContentView(view);
		d.show();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		releaseWakeLock();

		unBindBLeService();
	}

	WakeLock wakeLock = null;

	// 获取电源锁，保持该服务在屏幕熄灭时仍然获取CPU时，保持运行
	private void acquireWakeLock() {
		if (null == wakeLock) {
			PowerManager pm = (PowerManager) this.getSystemService(Context.POWER_SERVICE);
			wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, "PostLocationService");
			if (null != wakeLock) {
				wakeLock.acquire();
			}
		}
	}

	// 释放设备电源锁
	private void releaseWakeLock() {
		if (null != wakeLock) {
			wakeLock.release();
			wakeLock = null;
		}
	}
}
